﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class WebMaster_ManagementChange : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        LoadPage();
    }

    #region load page

    private void LoadPage()
    {
        string id = "";
        if (Request.Params[GlobalClassBE.MyUrlParameter.ObjectIDName] != null)
            id = Request.Params[GlobalClassBE.MyUrlParameter.ObjectIDName].ToString();

        GssDO.DOLogon DO = getByID(id);

        if (DO != null && DO.IsDeleted == 0)
        {
            pnlAddEdit.Visible = true;
            LoadInput(id);
            GlobalClassBE.MyMessage.MgsCurrentTitle = GlobalClassBE.PageName.ManagementProfileTitle;
        }
        else
        {
            Response.Redirect(@"Default.aspx", false);
        }
    }

    private void LoadInput(string id)
    {
        ShowTextForInput();
        if (!Page.IsPostBack)
        {
            GlobalClassBE.MyMessage.MgsCurrentMessages = "";

            ShowToEdit(id);
        }
    }

    #endregion load page

    #region show text

    private void ShowTextForInput()
    {
        lblUsername.Text = GlobalClassBE.LabelText.Username;
        lblPassword.Text = GlobalClassBE.LabelText.OldPassword;
        lblNewpassword.Text = GlobalClassBE.LabelText.NewPassword;
        lblNewPasswordConfirm.Text = GlobalClassBE.LabelText.ConfirmNewPassword;

        txtPassword.Focus();

        reqComparePassword.ErrorMessage = GlobalClassBE.MyMessage.InvalidCompareOldPassword;
        reqCompareNewPassword.ErrorMessage = GlobalClassBE.MyMessage.InvalidCompareNewPassword;
        reqField.Text = GlobalClassBE.MyMessage.MgsRequiredField;
        btnSave.Text = GlobalClassBE.ButtonText.BtnChangesPassword;
        btnCancel.Text = GlobalClassBE.ButtonText.BtnCancel;
    }

    #endregion show text

    #region ShowAll

    private void ShowToEdit(string pID)
    {
        try
        {
            GssDO.DOLogon DO = getByID(pID);

            if (DO != null)
            {
                lblName.Text = DO.Username;
                txtPassword.Text = "";
                txtNewPassword.Text = "";
                txtNewPasswordConfirm.Text = "";

                lblTempID.Text = pID;
                lblActionName.Text = GlobalClassBE.ActionText.EditPage;
                ShowButton(false);
                GlobalClassBE.MyMessage.MgsCurrentMessages = "";
            }
            else
            {
                GlobalClassBE.MyMessage.MgsCurrentMessages = GlobalClassBE.MyMessage.MgsEdit_Selected;
                CancelPage();
            }
        }
        catch (Exception exc)
        {
            string mess = exc.Message;
        }
    }
    private GssDO.DOLogon getByID(string id)
    {
        GssDO.DOLogon DO = null;
        try
        {
            GssBL.BLLogon BL = new GssBL.BLLogon();
            DO = BL.getLogonByID(id);
        }
        catch (Exception exc)
        {
            string mess = exc.Message.ToString();
            DO = null;
        }
        return DO;
    }
    /// <summary>
    /// show button on form (Parameter: true: show to add, false: show to edit)
    /// </summary>
    /// <param name="pFlag">type bool: true: show to add; false: show to edit</param>
    private void ShowButton(bool pFlag)
    {
        btnSave.Visible = true;
        btnCancel.Visible = true;
    }
    private void CancelPage()
    {
        Response.Redirect(@"Default.aspx", false);
    }
    private void SavePage(bool pFlag)
    {
        lblMessage.Text = "";
        if (validateForm())
        {
            string sID = lblTempID.Text.ToString();
            if (sID != "")
                SaveEdit(sID);
            else
            {
                GlobalClassBE.MyMessage.MgsCurrentMessages = GlobalClassBE.MyMessage.MgsEdit_Selected;
                CancelPage();
            }
        }
    }
    private void SaveEdit(string pID)
    {
        try
        {
            GssDO.DOLogon DO = fillDO(pID);
            GssBL.BLLogon BL = new GssBL.BLLogon();
            BL.Update(DO);
            GlobalClassBE.MyMessage.MgsCurrentMessages = GlobalClassBE.MyMessage.MgsEdit_Success;
            CancelPage();
        }
        catch (Exception exc)
        {
            string mess = exc.Message.ToString();
            lblMessage.Text = GlobalClassBE.MyMessage.MgsEdit_UnSuccess;
        }
    }

    private bool validateByLabel()
    {
        bool result = true;
        try
        {
            string sID = lblTempID.Text.Trim();
            GssDO.DOLogon DO = getByID(sID);
            if (DO != null)
            {
                string sPassword = GlobalClassBE.encodeMD5(txtPassword.Text.Trim());
                if (sPassword != DO.Password)
                {
                    result = false;
                    lblWrongPassword.Text = GlobalClassBE.MyMessage.InvalidOldPassword;
                }
                else
                {
                    lblWrongPassword.Text = "";
                }
            }
        }
        catch (Exception ex)
        {
            string error = ex.Message;
        }
        return result;
    }
    private bool validateForm()
    {
        bool result = true;
        try
        {
            if (!reqPassword.IsValid)
                result = false;
            if (!reqNewPassword.IsValid)
                result = false;
            if (!reqNewPasswordConfirm.IsValid)
                result = false;
            if (!reqComparePassword.IsValid)
                result = false;
            if (!reqCompareNewPassword.IsValid)
                result = false;

            if (result)
            {
                result = validateByLabel();
            }
        }
        catch (Exception ex)
        {
            string error = ex.Message;
        }
        return result;
    }

    private GssDO.DOLogon fillDO(string id)
    {
        GssDO.DOLogon DO = getByID(id);
        if (DO != null)
        {
            string sPassword = txtNewPassword.Text.Trim();
            DO.Password = GlobalClassBE.encodeMD5(sPassword);
        }
        return DO;
    }

    #endregion ShowAll

    /***** Add Edit ******/
    protected void btnSave_Click(object sender, EventArgs e)
    {
        SavePage(false);
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        CancelPage();
    }
}
